import { PrismaService } from 'nestjs-prisma'
import { Injectable } from '@nestjs/common'
import { Prisma } from '@prisma/client'
import { DomainModel } from '../model'

@Injectable()
export class MetaHistoryChartDao {
  get table() {
    return this.prismaService.metaHistoryChart
  }

  constructor(private readonly prismaService: PrismaService) {}

  /**
   * 添加数据
   * @param data
   * @returns
   */
  save(data: Prisma.MetaHistoryChartUncheckedCreateInput): Promise<DomainModel.MetaHistoryChart> {
    return this.table.create({
      data: {
        limitBeginTime: data.limitBeginTime,
        limitEndTime: data.limitEndTime,
        queryGranularity: data.queryGranularity,
        dataFillingStrategy: data.dataFillingStrategy,
        platform: data.platform,
      },
    })
  }

  /**
   * 根据ID更新数据
   * @param id
   * @param data
   * @returns
   */
  updateById(id: number, data: Prisma.MetaHistoryChartUpdateInput): Promise<DomainModel.MetaHistoryChart> {
    return this.table.update({
      data: {
        limitBeginTime: data.limitBeginTime,
        limitEndTime: data.limitEndTime,
        queryGranularity: data.queryGranularity,
        dataFillingStrategy: data.dataFillingStrategy,
      },
      where: { id },
    })
  }

  /**
   * 根据ID删除数据
   * @param id
   * @param deleteModel
   * @returns
   */
  async deleteById(id: number): Promise<void> {
    await this.table.delete({ where: { id } })
  }

  /**
   * 根据platform获取数据
   * @param param
   * @returns
   */
  getByPlatform(param: { platform: string }): Promise<DomainModel.MetaHistoryChart> {
    return this.table.findUnique({
      where: {
        platform: param.platform,
      },
    })
  }

  /**
   * 根据ID读取数据
   * @param id
   * @returns
   */
  getById(id: number): Promise<DomainModel.MetaHistoryChart> {
    return this.table.findUnique({
      where: { id },
    })
  }
}
